package com.biovr.backend.restcontroller;

import com.biovr.backend.domain.ExpmResult;
import com.biovr.backend.domain.UserArchive;
import com.biovr.backend.repository.ExpmResultRepository;
import com.biovr.backend.utils.DateTimeUtils;
import com.biovr.backend.utils.ResponseFactory;
import com.biovr.backend.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * Created by monodev on 2017/7/9.
 */
@RestController
@RequestMapping("/expmresult")
public class ExpmResultRestController {
    @Autowired
    private ExpmResultRepository expmResultRepository;
    @Autowired
    DateTimeUtils dateTimeUtils;

    /**
     * 根据用户id和实验id查找实验结果
     * @param userId
     * @param expmId
     * @return
     */
    @GetMapping
    public RestResponse<ExpmResult> getByUserAndExpm(@RequestParam String userId,@RequestParam String expmId){
        return ResponseFactory.createSuccessResult(expmResultRepository.findByUserIdAndExpmId(userId,expmId));
    }
    @PostMapping
    public  RestResponse<ExpmResult> addExpmResult(@RequestBody ExpmResult expmResult) throws Exception{
        ExpmResult tarResult = expmResultRepository.findByUserIdAndExpmId(expmResult.getUserId(),expmResult.getExpmId());
        //累加提交
        if(tarResult!=null){
            expmResult.setId(tarResult.getId());
        }
        expmResult.setReportTime( dateTimeUtils.getCurrDateTimeString());
        return ResponseFactory.createSuccessResult(expmResultRepository.save(expmResult));
    }

}
